package com.example.aircraftticketingsystemserver.controller;

import com.example.aircraftticketingsystemserver.entity.Order;
import com.example.aircraftticketingsystemserver.entity.OrderDTO;
import com.example.aircraftticketingsystemserver.entity.Ticket;
import com.example.aircraftticketingsystemserver.service.IOrdersService;
import com.example.aircraftticketingsystemserver.service.ITicketService;
import com.example.aircraftticketingsystemserver.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lxz
 * @since 2024-08-05
 */
@Transactional
@RestController
@CrossOrigin(origins = {"*","null"})
@RequestMapping("/order")
public class OrdersController {
    @Resource
    private IOrdersService ordersService;
    @Resource
    private ITicketService ticketService;

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order) {
        Ticket ticket = ticketService.selectTicketByTicketId(order.getTicketId());
        System.out.println(ticket);
        if (ticket.getCount() > 0) {
            try {
                // 添加订单
                LocalDateTime now = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                order.setBookingTime(now.format(formatter));
                ordersService.addOrder(order);
                // 订单插入成功后，减少票的数量
                Integer count = ticket.getCount();
                ticket.setCount(count - 1);
                System.out.println(ticket);
                ticketService.updateTicket(ticket);
                return Result.ok();
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("当前乘客信息已购买过该航班");
            }
        } else {
            return Result.error("票已售罄");
        }

    }
    /**
     * 分页查询
     * 根据用户id查询订单详情信息
     */
    @GetMapping("/getOrdersByUserId")
    public Result getOrdersByUserId(@RequestParam(value = "pageNo")Integer pageNo,
                                    @RequestParam(value = "pageSize")Integer pageSize,
                                    @RequestParam(value = "userId") Integer userId){
        return Result.ok(ordersService.getOrderDetailByUserId(pageNo, pageSize, userId));
    }
    /**
     * 改签
     */
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestBody OrderDTO orderDTO,@RequestParam Integer lastTicketId){
        System.out.println(orderDTO);
        System.out.println(lastTicketId);
        Order order=new Order();
        Ticket ticket=ticketService.selectTicketByTicketId(orderDTO.getTicketId());
        Integer count=ticket.getCount();
        if(count>0){
          try{
        Ticket ticket1=ticketService.selectTicketByTicketId(lastTicketId);
        Integer count1=ticket1.getCount();
        ticket1.setCount(count1+1);
        ticketService.updateTicket(ticket1);
        order.setStatus(orderDTO.getOrderStatus());
        order.setOrderId(orderDTO.getOrderId());
        order.setPassengerId(orderDTO.getPassengerId());
        order.setTicketId(orderDTO.getTicketId());
        order.setUserId(orderDTO.getUserId());
        order.setFlightId(orderDTO.getFlightId());
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        order.setBookingTime(now.format(formatter));
        ordersService.updateOrder(order);
        ticket.setCount(count-1);
        ticketService.updateTicket(ticket);
        return Result.ok();}
        catch  (Exception e) {
            e.printStackTrace();
            return Result.error("当前乘客信息已购买过该航班");
        }}
        else return Result.error("当前航班已售罄");
    }
    /**
     * 取消订单
     */
    @PostMapping("/returnTicket")
    public Result returnOrder(@RequestBody OrderDTO order){
        if(order.getOrderStatus().equals("已支付")){
            Order order1=new Order();
            order1.setOrderId(order.getOrderId());
            order1.setTicketId(order.getTicketId());
            order1.setBookingTime(order.getBookingTime());
            order1.setUserId(order.getUserId());
            order1.setStatus("已退款");
            order1.setPassengerId(order.getPassengerId());
            ordersService.updateOrder(order1);
            Ticket ticket=ticketService.selectTicketByTicketId(order.getTicketId());
            Integer count=ticket.getCount();
            ticket.setCount(count+1);
            ticketService.updateTicket(ticket);
            return Result.ok();
        }
        else return Result.error("该订单已取消");
    }

}
